package net.cjw.initsb.controller.background;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.dao.TopicCommentMapper;
import net.cjw.initsb.entity.Permission;
import net.cjw.initsb.entity.TopicComment;
import net.cjw.initsb.service.PermissionServiceI;
import net.cjw.initsb.service.TopicCommentServiceI;
@Api(description="评论")
@RestController
@RequestMapping("topic_comment")
public class TopicCommentController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private TopicCommentServiceI topicCommentServiceI;

	@PostMapping("v{version}/create")
	public ServiceResult<String> create(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "评论所属的主题id", defaultValue = "") @RequestParam String topic_id,
			@ApiParam(value = "主题类型 image video", defaultValue = "") @RequestParam String topic_type,
			@ApiParam(value = "被回复的评论id,评论主题传空", defaultValue = "") @RequestParam(required = false) String rid,
			@ApiParam(value = "被回复评论的uid,评论主题传空", defaultValue = "") @RequestParam(required = false) String ruid,
			@ApiParam(value = "被回复评论的级别,评论主题传空", defaultValue = "") @RequestParam(required = false) Integer rgrade,
			@ApiParam(value = "评论内容", defaultValue = "我评论内容") @RequestParam String content) {

		String uid = "admin";
		if (Strings.isNullOrEmpty(topic_id) || Strings.isNullOrEmpty(topic_type) || Strings.isNullOrEmpty(content)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			TopicComment topicComment = new TopicComment();
			topicComment.setTopicId(topic_id);
			topicComment.setTopicType(topic_type);
			topicComment.setRid(rid);
			topicComment.setRuid(ruid);
			int grade;
			if (rgrade == null) {
				grade = 1;
			} else {
				grade = rgrade + 1;
			}
			topicComment.setGrade(grade);
			topicComment.setContent(content);
			topicComment.setUid(uid);
			return topicCommentServiceI.create(topicComment);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

	@PostMapping("v{version}/update")
	public ServiceResult<String> update(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "评论id", defaultValue = "") @RequestParam String id,
			@ApiParam(value = "评论状态 0 删除 1审核 2 审核通过", defaultValue = "0") @RequestParam Integer comment_state) {
		if (Strings.isNullOrEmpty(id)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			TopicComment topicComment = new TopicComment();
			topicComment.setId(id);
			topicComment.setCommentSate(comment_state);
			return topicCommentServiceI.update(topicComment);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
